# Maintainer: Andrea Zagli <andrea.zagli.free@gmail.com>

_realname=meson
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.46.1
pkgrel=1
pkgdesc="High-productivity build system (mingw-w64)"
arch=('any')
url="http://mesonbuild.com/"
license=("Apache 2")
options=('strip' 'staticlibs')
depends=("${MINGW_PACKAGE_PREFIX}-python3"
         "${MINGW_PACKAGE_PREFIX}-ninja")
source=(https://github.com/mesonbuild/${_realname}/releases/download/${pkgver}/${_realname}-${pkgver}.tar.gz
        'color-term.patch'
        '0001-Install-scripts-without-.py-extension.patch'
        '0002-Default-to-sys.prefix-as-the-default-prefix.patch'
        '0003-Strip-the-prefix-from-all-paths-when-installing-with.patch'
        '0001-gtk-doc-Use-LD_LIBRARY_PATH-to-modify-PATH-on-W32.patch'
        )
sha256sums=('19497a03e7e5b303d8d11f98789a79aba59b5ad4a81bd00f4d099be0212cee78'
            '1615db5a0f776be83c4b6c004320d0df1dcdb71a161da37f580a129a322009a6'
            '2c3fbb9d3f367d97e7262dc2991a2ef36eb06a7af08227b9e525049471d968c5'
            '4ef1e6fb0e9bd927b4646e416631b8a0a9aece763d259dbba58168bcba93f673'
            '963a5203f3428d41e4a43ac2a42b6c9d47dcc8f3ccccbd11414fdf5ac8608e40'
            'f956f44fef088177c27ad8cb449281253ce48fc6b4a87abcec4f8da83866583d'
           )

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  patch -Np1 -i "${srcdir}"/color-term.patch

  patch -Np1 -i "${srcdir}"/0001-Install-scripts-without-.py-extension.patch
  patch -Np1 -i "${srcdir}"/0002-Default-to-sys.prefix-as-the-default-prefix.patch
  patch -Np1 -i "${srcdir}"/0003-Strip-the-prefix-from-all-paths-when-installing-with.patch

  # https://github.com/mesonbuild/meson/issues/3307
  patch -Np1 -i "${srcdir}"/0001-gtk-doc-Use-LD_LIBRARY_PATH-to-modify-PATH-on-W32.patch
}

build() {
  cd "${srcdir}/${_realname}-${pkgver}"

  ${MINGW_PREFIX}/bin/python3 setup.py build
}

package() {
  cd "${srcdir}/${_realname}-${pkgver}"

  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py \
    install --prefix=${MINGW_PREFIX} --root="${pkgdir}"

  PREFIX_WIN=$(cygpath -m ${MINGW_PREFIX})
  sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" \
    -i "${pkgdir}${MINGW_PREFIX}/bin/meson"
  sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" \
    -i "${pkgdir}${MINGW_PREFIX}/bin/mesonconf"
  sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" \
    -i "${pkgdir}${MINGW_PREFIX}/bin/mesonintrospect"
  sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" \
    -i "${pkgdir}${MINGW_PREFIX}/bin/mesontest"
  sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" \
    -i "${pkgdir}${MINGW_PREFIX}/bin/wraptool"

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
